package ltd.newbee.mall.controller.mall;


import ltd.newbee.mall.common.Constants;
import ltd.newbee.mall.controller.vo.NewBeeMallUserVO;
import ltd.newbee.mall.service.SchoolMallPointsService;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 会员积分控制
 */
@Controller

public class PersonalPointsController {

    @Autowired
    SchoolMallPointsService schoolMallPointsService;

    @RequestMapping("personalpoints")
    public String PersonalPointsPages(HttpServletRequest request,
                                      HttpSession httpSession){
        request.setAttribute("path", "personalpoints");
        NewBeeMallUserVO user = (NewBeeMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        request.setAttribute("points", schoolMallPointsService.findPoints(user.getUserId().intValue()));
        request.setAttribute("id", "123");
        return  "mall/personal-points";

    }

    @RequestMapping("findpoind")
    @ResponseBody
    public Result testpoint(){

        Result result = ResultGenerator.genFailResult(schoolMallPointsService.setPoint(9,40).toString());
        return result;
    }

    @RequestMapping("find")
    @ResponseBody
    public Result pointOrder(int goodsid,int points,HttpServletRequest request,
                             HttpSession httpSession){
        //1.获取用户id,修改用户积分
        //2.生成订单,积分兑换
        NewBeeMallUserVO user = (NewBeeMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
       int userpoint= schoolMallPointsService.findPoints(user.getUserId().intValue());
       int userpoint2 = userpoint-points;
       boolean isupdate= schoolMallPointsService.setPoint(user.getUserId().intValue(),userpoint2);
       if (isupdate){

       }
       return null;
    }

}
